const fs = require('fs');
const { promisify } = require('util');

/**
 * @param {string} str
 */
function escapeStringRegexp(str) {
  if (typeof str !== 'string') {
    throw new TypeError('Expected a string');
  }

  // Escape characters with special meaning either inside or outside character sets.
  // Use a simple backslash escape when it’s always valid, and a `\xnn` escape when the simpler form would be disallowed by Unicode patterns’ stricter grammar.
  return str.replace(/[|\\{}()[\]^$+*?.]/g, '\\$&').replace(/-/g, '\\x2d');
}

/**
 * @param {string} str
 */
function stringToRegex(str) {
  return new RegExp(escapeStringRegexp(str));
}
exports.readFileAsync = promisify(fs.readFile);
exports.writeFileAsync = promisify(fs.writeFile);
exports.existAsync = promisify(fs.exists);
exports.existSync = fs.existsSync;
exports.stringToRegex = stringToRegex;
